/**
 *
 */
package picounit.ruby;

import org.apache.bsf.BSFException;
import org.apache.bsf.BSFManager;
import org.jruby.javasupport.bsf.JRubyEngine;

import picounit.ruby.exception.InvalidExpression;

public class RubyBSFManager {
    public RubyBSFManager(String scriptFile) {
        this.context = scriptFile;

        this.manager = new BSFManager();

        new RubyScriptExecutor(manager).exec(scriptFile);
    }

    public Object eval(String expression) {
        try {
            return manager.eval("ruby", context, -1, -1, expression);
        }
        catch (BSFException bsfException) {
            throw new InvalidExpression(context, expression, bsfException);
        }
    }

    private final String context;
    private final BSFManager manager;

    static {
        BSFManager.registerScriptingEngine("ruby", JRubyEngine.class.getName(), new String[] { "rb" });
    }
}